﻿using Dos.ORM;
using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace orderCreater
{
    public class OrderCreaterModel
    {
        const string Post_WDH = "http://wdhweb.22ca.com/";
        const string Post_WSY = "http://531.22ca.com/";
        // 我订花-刷注册用户
        const string WDH_REGISTER = "mobile/paidan/register";
        // 我订花-更新购买次数
        const string WDH_UPDATE_BUY_COUNT = "mobile/paidan/update_buy_num";
        // 我订花-自产拿收货人信息
        const string WDH_ADDRESS_RANGE = "mobile/paidan/get_self_consignee";
        // 我订花-普通拿收货人信息
        const string WDH_ADDRESS_NORMAL = "mobile/paidan/get_common_consignee";
        // 我订花-订单备注
        const string WDH_ORDER_REMARK = "mobile/paidan/remark";
        // 我订花-增加收货人已使用次数
        const string WDH_ADDRESS_USED = "mobile/paidan/update_use_consignee";
        // 我订花-已存在未购买用户刷单
        const string WDH_EXIST_NOBUY_ORDER = "mobile/paidan/already_scalp";
        // 531-刷注册用户
        const string WSY_REGISTER = "admin/index.php?act=paidan_api&op=register";
        // 531-已存在未购买用户刷单 （更新ip和登录时间）
        const string WSY_EXIST_NOBUY_ORDER = "admin/index.php?act=paidan_api&op=already_scalp";
        // 531-更新购买次数
        const string WSY_UPDATE_BUY_COUNT = "admin/index.php?act=paidan_api&op=update_buy_num";
        // 531-自产拿收货人信息
        const string WSY_ADDRESS_RANGE = "admin/index.php?act=paidan_api&op=get_self_consignee";
        // 531-自产拿收货人信息
        const string WSY_ADDRESS_NORMAL = "admin/index.php?act=paidan_api&op=get_common_consignee";
        // 531-增加收货人已使用次数
        const string WSY_ADDRESS_USED = "admin/index.php?act=paidan_api&op=update_use_consignee";
        // 531-刷活跃度
        const string WSY_REFRESH_LOGIN = "admin/index.php?act=paidan_api&op=update_login";
        // 531-刷收藏
        const string WSY_REFRESH_COLLECT = "admin/index.php?act=paidan_api&op=collect_goods";
        // 531-订单备注
        const string WSY_ORDER_REMARK = "admin/index.php?act=paidan_api&op=remark";
        public OrderCreaterModel() 
        {
        }
        #region fun
        
        public WDH_REGISTER WSYNewUser()
        {
            ///获取新增买家信息
            string strReturn = MyPost(Post_WSY + WSY_REGISTER, "");
            return JsonHelper.DeserializeJsonToObject<WDH_REGISTER>(strReturn);
        }
        public WDH_REGISTER WSYExistUser()
        {
            ///获取已有买家信息
            string strReturn = MyPost(Post_WSY + WSY_EXIST_NOBUY_ORDER, "");
            return JsonHelper.DeserializeJsonToObject<WDH_REGISTER>(strReturn);
        }
        public WDH_AddrRange WSYNormalAddr()
        {
            ///普通拿收货人信息
            string strReturn = MyPost(Post_WSY + WSY_ADDRESS_NORMAL, "");
            return JsonHelper.DeserializeJsonToObject<WDH_AddrRange>(strReturn);
        }
        public WDH_AddrRange WSYRangeAddr(string area_ids)
        {
            ///自产拿收货人信息
            string strReturn = MyPost(Post_WSY + WSY_ADDRESS_RANGE, "area_ids=" + area_ids);
            return JsonHelper.DeserializeJsonToObject<WDH_AddrRange>(strReturn);
        }
        public Post_Msg WSYUpdateUserBuyCount(string member_id)
        {
            ///更新购买次数
            string strReturn = MyPost(Post_WSY + WSY_UPDATE_BUY_COUNT, "member_id=" + member_id);
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WSYOrderRemark()
        {
            ///获取订单备注
            string strReturn = MyPost(Post_WSY + WSY_ORDER_REMARK, "");
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WSYUpdateAddressCount(string id)
        {
            ///增加收货人已使用次数
            string strReturn = MyPost(Post_WSY + WSY_ADDRESS_USED, "id=" + id);
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WSYRefreshLogin()
        {
            ///刷活跃
            string strReturn = MyPost(Post_WSY + WSY_REFRESH_LOGIN, "");
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WSYRefreshCollect()
        {
            ///刷收藏
            string strReturn = MyPost(Post_WSY + WSY_REFRESH_COLLECT, "");
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public WDH_REGISTER WDHNewUser()
        {
            ///获取新增买家信息
            string strReturn = MyPost(Post_WDH + WDH_REGISTER, "");
            return JsonHelper.DeserializeJsonToObject<WDH_REGISTER>(strReturn);
        }
        public WDH_REGISTER WDHExistUser()
        {
            ///获取已有买家信息
            string strReturn = MyPost(Post_WDH + WDH_EXIST_NOBUY_ORDER, "");
            return JsonHelper.DeserializeJsonToObject<WDH_REGISTER>(strReturn);
        }
        public WDH_AddrNormal WDHNormalAddr()
        {
            ///普通拿收货人信息
            string strReturn = MyPost(Post_WDH + WDH_ADDRESS_NORMAL, "");
            return JsonHelper.DeserializeJsonToObject<WDH_AddrNormal>(strReturn);
        }
        public WDH_AddrRange WDHRangeAddr(string area_ids)
        {
            ///自产拿收货人信息
            string strReturn = MyPost(Post_WDH + WDH_ADDRESS_RANGE, "area_ids=" + area_ids);
            return JsonHelper.DeserializeJsonToObject<WDH_AddrRange>(strReturn);
        }
        public Post_Msg WDHUpdateUserBuyCount(string member_id)
        {
            ///更新购买次数
            string strReturn = MyPost(Post_WDH + WDH_UPDATE_BUY_COUNT, "member_id=" + member_id);
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WDHOrderRemark()
        {
            ///获取订单备注
            string strReturn = MyPost(Post_WDH + WDH_ORDER_REMARK, "");
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public Post_Msg WDHUpdateAddressCount(string id)
        {
            ///增加收货人已使用次数
            string strReturn = MyPost(Post_WDH + WDH_ADDRESS_USED, "id=" + id);
            return JsonHelper.DeserializeJsonToObject<Post_Msg>(strReturn);
        }
        public string getWDHOrderRemark()
        {
            Random rd = new Random();
            int orderRemarksRange = rd.Next(0, 100);
            string orderRemarks = "";
            if (orderRemarksRange % 10 > 6)
            {
                Post_Msg mmsg = WDHOrderRemark();
                if (mmsg.status == 200)
                {
                    orderRemarks = mmsg.data;
                }
            }
            return orderRemarks;
        }
        public string getWSYOrderRemark()
        {
            Random rd = new Random();
            int orderRemarksRange = rd.Next(0, 100);
            string orderRemarks = "";
            if (orderRemarksRange % 10 > 6)
            {
                Post_Msg mmsg = WSYOrderRemark();
                if (mmsg.status == 200)
                {
                    orderRemarks = mmsg.data;
                }
            }
            return orderRemarks;
        }
        #endregion













        public Byte[] StringToByte(String s, Encoding encoding)
        {
            return encoding.GetBytes(s);
        }
        /// <summary>
        /// 根据下单时间，生成配送时间
        /// </summary>
        /// <param name="OrderTime">下单时间</param>
        /// <returns>配送时间</returns>
        public string CreateSendTime(string OrderTime)
        {
            string returnString = string.Empty;
            string dateStr = DateTime.Parse(OrderTime).ToShortDateString();
            string timeStr = DateTime.Parse(OrderTime).Hour.ToString();
            Random rd = new Random();
            if (DateTime.Parse(OrderTime).Hour > 18)
            {
                timeStr = rd.Next(9, 21).ToString();
                dateStr = DateTime.Parse(OrderTime).AddDays(1).ToShortDateString();
            }
            else
            {
                timeStr = rd.Next(int.Parse(timeStr) + 2, 21).ToString();
            }

            returnString = dateStr + " " + timeStr + ":00:00";
            return returnString;
        }
        /// <summary>
        /// 根据下单时间和设置的付款时间范围，返回付款时间
        /// </summary>
        /// <param name="OrderTime">下单时间</param>
        /// <param name="PayRange">付款时间范围</param>
        /// <returns>付款时间</returns>
        public string CreatePayTime(string OrderTime, string PayRange)
        {
            int ttt = int.Parse(PayRange);
            Random rd = new Random();
            int addM = rd.Next(1, ttt);
            int addS = rd.Next(10, 50);
            DateTime newDate = DateTime.Parse(OrderTime).AddMinutes(addM).AddSeconds(addS);
            return newDate.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 向服务器请求数据
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="paramsStr">传递参数</param>
        /// <returns>服务端返回的数据</returns>
        public string MyPost(string url, string paramsStr)
        {
            try
            {
                //注意提交的编码 这边是需要改变的 这边默认的是Default：系统当前编码
                byte[] postData = Encoding.UTF8.GetBytes(paramsStr);

                // 设置提交的相关参数 
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                Encoding myEncoding = Encoding.UTF8;
                request.Method = "POST";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                request.ContentLength = postData.Length;

                // 提交请求数据 
                System.IO.Stream outputStream = request.GetRequestStream();
                outputStream.Write(postData, 0, postData.Length);
                outputStream.Close();

                HttpWebResponse response;
                Stream responseStream;
                StreamReader reader;
                string srcString;
                response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                srcString = reader.ReadToEnd();
                string result = srcString;   //返回值赋值
                reader.Close();
                return result;
            }
            catch
            {
                return "error";
            }
        }
        /// <summary>
        /// 生成订单ID
        /// </summary>
        /// <returns>订单ID</returns>
        public string CreateOrderCode()
        {
            string sn = string.Empty;
            string r1 = string.Empty;
            string r2 = string.Empty;
            Random ra = new Random();
            r1 = ra.Next(10000, 99999).ToString();
            r2 = ra.Next(10000, 99999).ToString();
            sn = new string(DateTime.Now.ToFileTimeUtc().ToString().ToCharArray().OrderBy(s => Guid.NewGuid().ToString()).ToArray());
            sn = r1.Substring(0, 3) + sn.Substring(0, 4) + r2.Substring(0, 4) + sn.Substring(5, 5);
            return sn;
        }
        /// <summary>
        /// 生成付款ID
        /// </summary>
        /// <param name="memberId">下单人ID</param>
        /// <returns>付款ID</returns>
        public string createPayCode(string memberId)
        {
            string sn = string.Empty;
            string r1 = string.Empty;
            string r2 = string.Empty;
            Random ra = new Random();
            r1 = ra.Next(10000, 99999).ToString();
            r2 = ra.Next(10000, 99999).ToString();
            sn = new string(DateTime.Now.ToFileTimeUtc().ToString().ToCharArray().OrderBy(s => Guid.NewGuid().ToString()).ToArray());
            sn = r1.Substring(0, 3) + sn.Substring(0, 4) + r2.Substring(1, 3) + sn.Substring(5, 5) + sn.Substring(3, 3);
            return sn.Substring(0, 18 - memberId.Length) + memberId;
        }
        /// <summary>
        /// 中文转拼音
        /// </summary>
        /// <param name="str">中文</param>
        /// <returns>拼音</returns>
        public string GetPinyin(string str)
        {
            string r = String.Empty;
            foreach (char obj in str)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(obj);
                    string t = chineseChar.Pinyins[0].ToString();
                    r += t.Substring(0, t.Length - 1);
                }
                catch (Exception)
                {

                    r += obj.ToString();
                }
            }
            r = r.ToLower();

            Random rd = new Random();
            string[] arrUserName = new string[] { "00", "5", "@", ".", "#", "2", "88", "&", "o", "168" };
            int rdKey = rd.Next(0, 10);

            r = r + arrUserName[rdKey] + rd.Next(1111, 9999).ToString();
            return r;
        }
        /// <summary>
        /// 随机生成邮箱名
        /// </summary>
        /// <param name="name">注册名</param>
        /// <returns>邮箱名</returns>
        public string getRandEmail(string name)
        {
            string rtn = "";

            Random rd = new Random();

            int key = rd.Next(0, 17);

            rtn = name;

            switch (key)
            {
                case 0: rtn = rtn + "@126.com"; break;
                case 1: rtn = rtn + "@163.com"; break;
                case 2: rtn = rtn + "@gmail.com"; break;
                case 3: rtn = rtn + "@yeah.net"; break;
                case 4: rtn = rtn + "@sina.com"; break;
                case 5: rtn = rtn + "@qq.com"; break;
                case 6: rtn = rtn + "@tom.com"; break;
                case 7: rtn = rtn + "@eyou.com"; break;
                case 8: rtn = rtn + "@21cn.com"; break;
                case 9: rtn = rtn + "@yahoo.com.cn"; break;
                case 10: rtn = rtn + "@56.com"; break;
                case 11: rtn = rtn + "@live.com"; break;
                case 12: rtn = rtn + "@163.net"; break;
                case 13: rtn = rtn + "@3721.net"; break;
                case 14: rtn = rtn + "@aim.com"; break;
                case 15: rtn = rtn + "@walla.com"; break;
                case 16: rtn = rtn + "@inbox.com"; break;
            }

            return rtn;
        }
        /// <summary>
        /// 随机返回支付类型
        /// </summary>
        /// <returns>支付类型</returns>
        public string CreatePayType()
        {
            string[] arrUserName = new string[] { "alipay", "alipay", "bankremit", "bankremit", "bankremit", "alipay", "bankremit", "bankremit", "alipay", "bankremit", "bankremit" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey];
        }
        /// <summary>
        /// 随机支付需要支付
        /// </summary>
        /// <returns>1：需要，0：不需要</returns>
        public bool IsPay()
        {
            string[] arrUserName = new string[] { "0", "1", "1", "1", "0", "1", "1", "1", "1", "0" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey] == "1";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string shiDuan()
        {
            string[] arrUserName = new string[] { "2", "2", "1", "1", "1", "1", "1", "1", "1", "3" };
            Random rd = new Random();
            int rdKey = rd.Next(0, 10);
            return arrUserName[rdKey];
        }
        /// <summary>
        /// 生成订单时间
        /// </summary>
        /// <param name="shortDateStr">时间</param>
        /// <returns></returns>
        public string CreateOrderTime(string shortDateStr)
        {
            Random random = new Random();
            string str = this.shiDuan();
            //string str3 = this.sr_dt_time.Value.ToShortDateString();
            string str4 = random.Next(0, 0x10).ToString();
            switch (str)
            {
                case "2":
                    str4 = random.Next(0x13, 0x18).ToString();
                    break;

                case "3":
                    str4 = random.Next(0, 10).ToString();
                    break;
            }
            string str5 = random.Next(0, 20).ToString();
            string str6 = random.Next(0, 60).ToString();
            if (str5.Length < 2)
            {
                str5 = "0" + str5;
            }
            if (str6.Length < 2)
            {
                str6 = "0" + str6;
            }
            string[] textArray1 = new string[] { shortDateStr, " ", str4, ":", str5, ":", str6 };
            return string.Concat(textArray1);
        }
    }

    public class Post_Msg
    {
        public string data { get; set; }
        public string msg { get; set; }
        public int status { get; set; }
    }
    public class WDH_USER
    {
        public string member_id { get; set; }
        public string member_name { get; set; }
        public string ali_account { get; set; }
        public string member_truename { get; set; }
    }
    public class WDH_REGISTER
    {
        public WDH_REGISTER()
        {
            data = new WDH_USER();
        }
        public WDH_USER data { get; set ; } 
        public string msg { get; set; }
        public int status { get; set; }
    }
    public class WDH_Address
    {
        public string id { get; set; }
        public string name { get; set; }
        public string mobile { get; set; }
        public string email { get; set; }
        public string address { get; set; }
        public string sex { get; set; }
        public string province_id { get; set; }
        public string city_id { get; set; }
        public string area_id { get; set; }
        public string use_count { get; set; }
        public string handle_state { get; set; }
        public string handle_msg { get; set; }
    }
    public class WDH_AddrNormal
    {
        public WDH_AddrNormal()
        {
            data = new List<WDH_Address>();
        }
        public List<WDH_Address> data { get; set; }
        public string msg { get; set; }
        public int status { get; set; }
    }
    public class WDH_Address2
    {
        public string id { get; set; }
        public string name { get; set; }
        public string mobile { get; set; }
        public string email { get; set; }
        public string address { get; set; }
        public string sex { get; set; }
        public string province_id { get; set; }
        public string city_id { get; set; }
        public string area_id { get; set; }
    }
    public class WDH_AddrRange
    {
        public WDH_AddrRange()
        {
            data = new List<WDH_Address2>();
        }
        public List<WDH_Address2> data { get; set; }
        public string msg { get; set; }
        public int status { get; set; }
    }

    public class WDH_Goods
    {
        public string goodsId { get; set; }
        public string goodsPrice { get; set; }
        public string goodsName { get; set; }
        public string goodsLogo { get; set; }
        public string goodsSub { get; set; }

    }
    public class WSY_Goods
    {
        public string goods_id { get; set; }
        public string goods_price { get; set; }
        public string goods_name { get; set; }
        public string gc_id { get; set; }
        public string goods_image { get; set; }
        
    }
    public class WDH_Buyer
    {
        public string memberMobile { get; set; }
        public string realName { get; set; }
    }
    public class WSY_Buyer
    {
        public string member_name { get; set; }
        public string member_email { get; set; }
    }
    public class WSY_Shop
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }
}
